package com.medical.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.medical.HttpTools.DataAnalyze;
import com.medical.HttpTools.HttpRequest;
import com.medical.po.HospitalberthCustom;
import com.medical.service.HospitalBerthService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年10月10日 下午1:23:59 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/OrderMan")
public class OrderController {
	@Autowired
	private HospitalBerthService hospitalBerthService;
	/**
	 * 医院订单界面，显示医院和医生以及病人间的订单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/MyOrder")
	public ModelAndView MyOrder()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/OrderMan/MyOrder");
		return model;
	}
	/**
	 * 医院订单结算界面，显示所有未结算的订单，Order by OrderTime DESC
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/OrderSettle")
	public ModelAndView OrderSettle()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/OrderMan/OrderSettle");
		return model;
	}
	/**
	 * 订单结算提交
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/OrderSettleSubmit")
	public @ResponseBody Map<String,Object> OrderSettleSubmit(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		String userorderid=request.getParameter("userorderid");
		String userorderhprice=request.getParameter("userorderhprice");
		String Param = "hosploginid="+hosploginid+"&userorderid="+userorderid+"&userorderhprice="+userorderhprice;
		System.out.println("param="+Param);
		Map<String, Object> SettleOrderJson = new HashMap<>();
		if(hosploginid!=null && userorderid!=null && userorderhprice!=null){
			JSONObject json = HttpRequest.sendPost("settleuserorder",Param);
			System.out.println("医院确认订单结算返回json："+json);
	        Integer code = Integer.parseInt(json.get("code").toString());
	        String msg = json.get("msg").toString();
	        
	        SettleOrderJson.put("code", code);
	        SettleOrderJson.put("msg", msg);
		}else{
			 SettleOrderJson.put("code", -100);
		     SettleOrderJson.put("msg", "订单结算失败，检查提交数据或登录过期！");
		}
		return SettleOrderJson;
	}
	/**
	 * 订单界面订单table异步ajax加载
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/OrderAjax")
	public @ResponseBody Map<String,Object> OrderAjax (Integer limit,Integer offset,String type,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+hosploginid);
		
		String OrderParam = "limit="+limit+"&offset="+offset+"&type="+type+"&hosploginid="+hosploginid;//参数串
		String jsonobjectmsg = "";
		String jsonobjectcode ="";
		Integer total=0;
		List<Map<String,Object>> orderlist=new ArrayList<>();
		if(hosploginid!=null) {
			try {
				JSONObject OrderListjson = HttpRequest.sendPost("listuserorder",OrderParam);
				System.out.println("输出接收到的json数据是："+OrderListjson);
				jsonobjectmsg = OrderListjson.get("msg").toString();
				jsonobjectcode =OrderListjson.get("code").toString();
				if("100".equals(jsonobjectcode)) {
					try {
						if(OrderListjson.get("data").toString()!="null") {
							//System.out.println("data:"+OrderListjson.get("data"));
							JSONObject jsonobjectdata =OrderListjson.getJSONObject("data");
							JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
							total = Integer.parseInt(jsonobjectdata.get("total").toString());
							orderlist = DataAnalyze.UserOrderJsonToList(jsonarrayrows);
						}
					} catch (Exception e) {
						System.out.println("出错信息是："+e.getMessage());
						jsonobjectmsg = "数据解析错误";
						jsonobjectcode ="-300";
					}
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				jsonobjectmsg = "数据传输错误";
				jsonobjectcode ="-200";
			}
		}else {
			jsonobjectmsg = "请先登录";
			jsonobjectcode ="-100";
		}
		//System.out.println("输出转接的jsonarrayrows："+jsonarrayrows);
		//System.out.println("输出转换后的orderlist:"+orderlist);
		
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", orderlist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
		return map;
	}
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/CheckDeptEmpty")
	public @ResponseBody int CheckDeptEmpty(HttpServletRequest request) throws Exception{
//		System.out.println("检查到的数据是这样的："+hospitalberthCustom.getHospdeptname());
//		int number=hospitalBerthService.CheckdeptberthEmpty(hospitalberthCustom);
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String userorderhospprimarydept = request.getParameter("userorderhospprimarydept");
		String userorderhospseconddept = request.getParameter("userorderhospseconddept");
		String param = "hosploginid="+hosploginid+"&userorderhospprimarydept="+userorderhospprimarydept+"&userorderhospseconddept="+userorderhospseconddept;
		Integer number=0;
		try {
			JSONObject json = HttpRequest.sendPost("checkberthempty", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			if("100".equals(jsoncode)) {
				JSONObject data = json.getJSONObject("data");
				number = data.getInt("number");
			}
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
		}
		System.out.println("返回的床位数量："+number);
		return number;
	}
	/**
	 * 医院端确认病人提交的住院订单，并输入押金，等待病人端确认
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/AccepteOrder")
	public @ResponseBody Map<String, Object> AccepteOrder(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		String userorderid = request.getParameter("userorderid");
		String userorderdeposit = request.getParameter("userorderdeposit");
		String userorderhospprimarydept = request.getParameter("userorderhospprimarydept");
		String userorderhospseconddept = request.getParameter("userorderhospseconddept");
		
		System.out.println("userorderid="+userorderid+"userorderdeposit="+userorderdeposit);
		String Param="hosploginid="+hosploginid+"&userorderid="+userorderid+"&userorderdeposit="+userorderdeposit+"&userorderhospprimarydept="+userorderhospprimarydept+"&userorderhospseconddept="+userorderhospseconddept;
		System.out.println("Param="+Param);
		Map<String, Object> orderResponseJson = new HashMap<>();
		if(hosploginid!=null && userorderid!=null && userorderdeposit!=null && userorderhospprimarydept!=null ){
			JSONObject json = HttpRequest.sendPost("confirmuserorder",Param);
			System.out.println("医院确认订单返回状态码："+json);
	        Integer code = Integer.parseInt(json.get("code").toString());
	        String msg = json.get("msg").toString();
	        
	        orderResponseJson.put("code", code);
	        orderResponseJson.put("msg", msg);
		}else {
			orderResponseJson.put("code", -100);
			orderResponseJson.put("msg", "检查提交数据，不可为空！或刷新页面，重新提交");
		}
		return orderResponseJson;
	}
	/**
	 * 医院端拒绝订单，使得订单作废
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/RefuseOrder")
	public @ResponseBody Map<String,Object> RefuseOrder(HttpServletRequest request)throws Exception{
		String userorderid=request.getParameter("userorderid");
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String Param="hosploginid="+hosploginid+"&userorderid="+userorderid;
		Map<String, Object> OrderRefuseJson = new HashMap<>();
		if(hosploginid!=null &&userorderid!=null){
			JSONObject json = HttpRequest.sendPost("refuseuserorder",Param);
			System.out.println("医院确认订单返回状态码："+json);
	        Integer code = Integer.parseInt(json.get("code").toString());
	        String msg = json.get("msg").toString();
	        
	        OrderRefuseJson.put("code", code);
			OrderRefuseJson.put("msg", msg);
		}else {
			OrderRefuseJson.put("code", -100);
			OrderRefuseJson.put("msg", "订单拒绝失败！");
		}
		return OrderRefuseJson;
	}
	/**
	 * 如果首次填写的押金不够，医院端可以增加押金，订单状态再次变为等待病人付款
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="AddDeposit")
	public @ResponseBody Map<String,Object> AddDeposit(HttpServletRequest request)throws Exception{
		String userorderid=request.getParameter("userorderid");
		String userorderdeposit=request.getParameter("userorderdeposit");
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String Param="hosploginid="+hosploginid+"&userorderid="+userorderid+"&userorderdeposit="+userorderdeposit;
		Map<String, Object> AddDepositJson = new HashMap<>();
		if(hosploginid!=null && userorderid!=null && userorderdeposit!=null){
			JSONObject json = HttpRequest.sendPost("adddeposit",Param);
			System.out.println("医院确认押金增加返回json："+json);
	        Integer code = Integer.parseInt(json.get("code").toString());
	        String msg = json.get("msg").toString();
	        
	        AddDepositJson.put("code", code);
	        AddDepositJson.put("msg", msg);
		}else {
			AddDepositJson.put("code", -100);
			AddDepositJson.put("msg", "押金增加失败，检查提交数据！");
		}
		return AddDepositJson;
	}
	
	/**
	 * 测试用例
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ToastrNotifications")
	public ModelAndView Toastr_Notifications()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/OrderMan/ToastrNotifications");
		return model;
	}
	@RequestMapping(value="/NewMessage")
	public @ResponseBody Map<String,Object> NewMassage()throws Exception{
		Map<String, Object> message = new HashMap<>();
		message.put("code", 100);
		message.put("msg", "its a new message from ajax by url Main/OrderMan/NewMessage!");
		message.put("title", "new message");
		return message;
	}
}
